package com.nitty.cash.loop.utils.helper

import com.nitty.cash.loop.NittyApp
import com.nitty.cash.loop.configs.NittyApiPath
import com.nitty.cash.loop.configs.NittyApiUrl
import com.nitty.cash.loop.configs.NittyConfigs
import com.nitty.cash.loop.sdk.analyse.AnalyseBuilder
import com.nitty.cash.loop.sdk.analyse.AnalyseUtil
import com.nitty.cash.loop.sdk.device.NittyDeviceSycBuilder
import com.nitty.cash.loop.sdk.device.NittyDeviceSycManager
import com.nitty.cash.loop.utils.shared.NittySharedHelper
import com.nitty.cash.loop.utils.shared.NittySharedKey

/**
 * TODO: Analysis Lib 初始化时机
 */
object NittyAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalyseBuilder.product = NittyConfigs.NITTY_APPNAME
        AnalyseBuilder.appVersion = "1"
        AnalyseBuilder.appPckName = NittyConfigs.NITTY_PACKAGENANME

        AnalyseBuilder.aesKey = NittyConfigs.getSecretKey()
        AnalyseBuilder.aesIv = NittyConfigs.getSecretIv()

        AnalyseBuilder.host = NittyApiUrl.NITTY_HOST
        AnalyseBuilder.zone = ""
        AnalyseBuilder.pathCommon = NittyApiUrl.getRandomUrl()

        AnalyseBuilder.pathGoogleInstall = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_GOOGLE_INSTALL_PATH
        AnalyseBuilder.pathAdjustInstall = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_ADJUST_INSTALL_PATH
        AnalyseBuilder.pathFacebookInstall = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_FACEBOOK_INSTALL_PATH

        AnalyseUtil.init(NittyApp.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalyseBuilder.zone = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TIMEZONE)
        AnalyseUtil.init(NittyApp.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalyseBuilder.token = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN)
        AnalyseBuilder.userId = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_USERID)
        AnalyseUtil.init(NittyApp.app)
    }
}

/**
 * TODO: Analysis Lib 打点时机
 */
object TestAnalysisAddEvent {

    /**
     * TODO: 在调用登录接口，并成功返回时，如果 type=="REGISTER"，则调用注册事件打点
     */
    fun addRegisterEvent() {
        AnalyseUtil.addRegisterEvent()
    }

    /**
     * TODO: 在调用提交贷款申请接口，并成功返回时，则调用申请事件打点
     */
    fun addApplyEvent() {
        AnalyseUtil.addApplyEvent()
    }
}

/**
 * TODO: Data Lib 初始化时机
 */
object TestDataLibInit {

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 data lib
     */
    fun initDataLibAtZoneSuccess() {
        NittyDeviceSycBuilder.aesKey = NittyConfigs.getSecretKey()
        NittyDeviceSycBuilder.aesIv = NittyConfigs.getSecretIv()

        NittyDeviceSycBuilder.product = NittyConfigs.NITTY_APPNAME
        NittyDeviceSycBuilder.appid = NittyConfigs.NITTY_APPNAME

        NittyDeviceSycBuilder.advertId = AnalyseUtil.getAdId()
        NittyDeviceSycBuilder.androidId = AnalyseUtil.getAndroidId()

        NittyDeviceSycBuilder.host = NittyApiUrl.NITTY_HOST
        NittyDeviceSycBuilder.zone = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TIMEZONE)
        NittyDeviceSycBuilder.pathCommon = NittyApiUrl.getRandomUrl()
        NittyDeviceSycBuilder.pathApl = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_APPLIST_PATH
        NittyDeviceSycBuilder.pathMag = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_SMS_PATH
        NittyDeviceSycBuilder.pathDie = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_DEVICE_PATH
        NittyDeviceSycBuilder.pathBty = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_BATTERY_PATH
        NittyDeviceSycBuilder.pathHdw = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_HARDWAVE_PATH
        NittyDeviceSycBuilder.pathContat = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_CONTACT_PATH
        NittyDeviceSycBuilder.pathSto = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_STORGE_PATH
        NittyDeviceSycBuilder.pathNtw = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_WIFI_PATH
        NittyDeviceSycBuilder.pathCal = NittyApiPath.NITTY_ANALYSIS.NITTY_ANALYSIS_CALLLOG_PATH

        NittyDeviceSycManager.init(NittyApp.app)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     */
    fun sycData() {
        val token = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN)
        NittyDeviceSycBuilder.advertId = AnalyseUtil.getAdId()
        NittyDeviceSycBuilder.androidId = AnalyseUtil.getAndroidId()

        NittyDeviceSycManager.syncData(token = token) {
            // 当前业务可忽略这个回调
        }
    }

    /**
     * TODO: 在申请 PHONE_STATE 权限之后上报 (进入首页上报) 包含：HARDWARE、STORE、DEVICE
     */
    fun sycPhoneStateStoreDeviceHardwareData(){
        val token = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN)
        NittyDeviceSycBuilder.advertId = AnalyseUtil.getAdId()
        NittyDeviceSycBuilder.androidId = AnalyseUtil.getAndroidId()
        NittyDeviceSycManager.sycPhoneStateStoreDeviceHardwareData(token = token) {
            // 当前业务可忽略这个回调
        }
    }

    /**
     * TODO: 在申请 PHONE_STATE 权限之后上报 包含：Battery、WIFI
     */
    fun sycPhoneStateBatteryWifiData(){
        val token = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN)
        NittyDeviceSycBuilder.advertId = AnalyseUtil.getAdId()
        NittyDeviceSycBuilder.androidId = AnalyseUtil.getAndroidId()
        NittyDeviceSycManager.sycPhoneStateBatteryWifiData(token = token) {
            // 当前业务可忽略这个回调
        }
    }

    /**
     * TODO: 在申请 READ_SMS 权限之后上报 包含：AppList、Sms
     * 同意sms权限之后
     */
    fun sycReadSmsAppListSmsData(){
        val token = NittySharedHelper.getNittySharedString(NittySharedKey.NITTY_TOKEN)
        NittyDeviceSycBuilder.advertId = AnalyseUtil.getAdId()
        NittyDeviceSycBuilder.androidId = AnalyseUtil.getAndroidId()
        NittyDeviceSycManager.sycReadSmsAppListSmsData(token = token) {
            // 当前业务可忽略这个回调
        }
    }
}